/**
 * 建委官网施工许可证信息
 *  
 */
import {Base,Item} from '../base';
class jwgwsg extends Base{
  constructor(){
      super();
      this.name='建委官网施工许可证信息';
      this.enabled = true;
      this.encode = "utf-8";
      this.debug = false;
      this.timeout = 1000000;
      this.collection = "lnxm";
      this.collectionShowName="辽宁项目爬虫";
      this.keyField = "title";
  }
  getItems(pageIndex:number) :Item[] {
      let url=`http://www.syjs.gov.cn/xxgk/97/101.aspx`;
      (<any>this.spider).headers = {};
      let content =this.spider.getSync(url);
      let $ =this.cheerio.load(content);
      let result  :Item[] =[];
      let that =this;
        try {
            for(let i=4;i<=6;i++){
               let title =$('.news_list').find('li').eq(i).find('a').attr('title');
               let url= 'http://www.syjs.gov.cn'+ $('.news_list').find('li').eq(i).find('a').attr('href');
               result=result.concat(this.getExtraItem(url,title));
            }
        } catch (error) {
            console.error(error);
        }
        return result;
  }
  getExtraItem(url:string, title:string) {
    let result  :Item[] =[];
    let content = this.spider.getSync(url);
    let $ = this.cheerio.load(content);
    let that = this;
    let first=$('table tr').first();
    $('table tr').not(first).each(function (index, ele) {
        let item = that.buildItem();
        $('table tr').eq(0).find('td').each(function (index1, ele1) {
            (<any>item)[$(ele1).text().trim()] = $(ele).find('td').eq(index1).text().trim();
        });
        (<any>item).url = url;
        item.source = that.name;
        item.title = title;
        result.push(item);
    });
    return result;
}
  getPageCount ():number{
      return 1;
  }
}
module.exports=new jwgwsg();